package com.zhaogang.servlet;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.zhaogang.employee.Employee;
import com.zhaogang.service.IEmployeeService;
import com.zhaogang.service.impl.EmployeeServiceImpl;

public class UpdateServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private IEmployeeService service = new EmployeeServiceImpl();

	public UpdateServlet() {
		super();

	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String id2 = request.getParameter("id");
		Integer id = Integer.parseInt(id2 == null || "".equals(id2) ? "0" : id2);
		
		Employee employee = service.queryObject(id);
		request.setAttribute("em", employee); 
		request.getRequestDispatcher("/update.jsp").forward(request, response);
		return;
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Employee employee = new Employee();
		String id2 = request.getParameter("id");
		
		Integer id = Integer.parseInt(id2 == null || "".equals(id2) ? "0" : id2);
		employee.setId(id);
		String name = request.getParameter("name");
		employee.setName(name);
		Integer age = Integer.parseInt(request.getParameter("age"));
		employee.setAge(age);
		Double salary = Double.parseDouble(request.getParameter("salary"));
		employee.setSalary(salary);
	SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		java.util.Date date = null;
		try {
			date = format.parse(request.getParameter("hireDate"));
			java.sql.Date hireDate = new java.sql.Date(date.getTime());
			employee.setHireDate(hireDate);
			service.updateObject(employee);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		response.sendRedirect("employee");
	}

}
